﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TradeTest
{
    public class SIIMClient
    {
        [DllImport("MediSIIM.Client.dll", CharSet = CharSet.Ansi)]
        static extern int InitClient(string sAddr, int iTimeOut);
        [DllImport("MediSIIM.Client.dll", CharSet = CharSet.Ansi)]
        static extern int RunService([MarshalAs(UnmanagedType.VBByRefStr)] ref string msgType, [MarshalAs(UnmanagedType.VBByRefStr)] ref string version, [MarshalAs(UnmanagedType.VBByRefStr)] ref string inxml, int inLen, [MarshalAs(UnmanagedType.VBByRefStr)] ref string outxml, [MarshalAs(UnmanagedType.I4)] ref int outLen);
        [DllImport("MediSIIM.Client.dll", CharSet = CharSet.Ansi)]
        static extern int ConvertFromUTF8([MarshalAs(UnmanagedType.VBByRefStr)] ref string pSource, int srclen, [MarshalAs(UnmanagedType.VBByRefStr)] ref string pTarget, int targetlen);

        public static void run()
        {
            var retcode = InitClient("tcp://127.0.0.1:20000", 30);
            string msgType = "HangZhouSYB.DUKA";
            string version = "v2_001";
            string inxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ZIFUBL_IN>  <BASEINFO>    <CAOZUOYDM>0060</CAOZUOYDM>    <CAOZUOYXM>厉红</CAOZUOYXM>    <CAOZUORQ>2012-07-26 18:13:41</CAOZUORQ> <XITONGBS>3</XITONGBS>    <FENYUANDM>0</FENYUANDM>  </BASEINFO>  <ZIFUBL>    <JIESUANLB>03</JIESUANLB>    <YILIAOLB>00</YILIAOLB>    <RENYUANLB>11</RENYUANLB>    <DAIYULB>1</DAIYULB>   <BINGRENID>50933</BINGRENID>    <FEIYONGLX>1</FEIYONGLX>    <XIANGMUDM>8722</XIANGMUDM>    <CHANDIDM>2895</CHANDIDM>    <FEIYONGRQ>2012-07-16 00:00:00</FEIYONGRQ>   <XIANGMUDJ>4.0000</XIANGMUDJ>    <ZIFEIBZ></ZIFEIBZ>    <SHIYONGZLXH>-1</SHIYONGZLXH>    <XIANGMUGL>13</XIANGMUGL>  </ZIFUBL></ZIFUBL_IN> ";            
            string outxml = string.Empty.PadRight(10240);
            int outLen = 10240;
            var ret = RunService(ref msgType, ref version, ref inxml, Encoding.UTF8.GetByteCount(inxml), ref outxml, ref outLen);
            System.Windows.Forms.MessageBox.Show(Encoding.UTF8.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Default, Encoding.UTF8.GetBytes(outxml.TrimEnd(' ', '\0')))));
            string tg = string.Empty.PadRight(10240);
            ConvertFromUTF8(ref outxml, outLen, ref tg, 10240);
        }
    }
}
